import {type Datafeed, type SymbolInfo, type Period, type DatafeedWatchCallback, type KData, type GetKlineArgs} from "~/composables/types";
import {$fetch} from "ofetch";

export default class MyDatafeed implements Datafeed{
  
  async getHistoryKLineData({symbol, period, from, to, strategy}: GetKlineArgs): Promise<KData> {
    const query = {symbol: symbol.ticker, timeframe: period.timeframe, exchange: symbol.exchange, from, to}
    const rsp = await $fetch('/api/kline/hist', {query})
    const data = await (rsp.data || []).map((data: any) => ({
      timestamp: data[0],
      open: data[1],
      high: data[2],
      low: data[3],
      close: data[4],
      volume: data[5]
    }))
    return {data}
  }

  async getSymbols(): Promise<SymbolInfo[]> {
    const rsp = await $fetch(`/api/kline/symbols`)
    return await (rsp.data || []).map((data: any) => ({
      ticker: data.symbol,
      name: data.symbol,
      shortName: data.symbol,
      market: 'spot',
      exchange: data.exchange,
      priceCurrency: 'USDT',
      type: data.type,
      logo: ''
    }))
  }

  subscribe(symbol: SymbolInfo, period: Period, callback: DatafeedWatchCallback): void {
  }

  unsubscribe(symbol: SymbolInfo, period: Period): void {
  }

}